package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author ming
 * @create 2022/5/13
 * @description: 秒杀商品定时上架
 * 每天晚上3点,上架最近三天
 * 当天 00:00:00-23:59:59
 * 明天:00:00:00-23:59:59
 * 后天 00:00:00-23:59:59
 */
@Service
@Slf4j
public class SeckillSkuScheduled {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;
    public static final String upload_lock = "seckill:upload:lock";
    @Scheduled(cron="*/3 * * * * ?")
    public void uploadSeckillLate3Day(){
            log.info("***************上架秒杀商品**************");
            RLock lock = redissonClient.getLock(upload_lock);
        try {
            //重复上架无需处理
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSeckillLate3Day();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
